package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {


    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key2 = (String) params.get("key");

        LambdaQueryWrapper<BrandEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(key2)) {
            // 一个关键词可以查询多个内容
            lambdaQueryWrapper.eq(BrandEntity::getBrandId, key2).or().like(BrandEntity::getName, key2).or().like(BrandEntity::getDescript, key2);
        }
        IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params), lambdaQueryWrapper);

        return new PageUtils(page);
    }

    // 还要更新我们的品牌和分类关系表里面的内容
    // TODO 我们更改了brandName 然后 categoryBrandRelation 里面的2brandName 也进行修改
    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        // 保证庸于字段
        boolean b = updateById(brand);

        // 判断是否更改品牌名字 如果更改了的话就要进行品牌和分类那个表名字的更新
        if (!StringUtils.isEmpty(brand.getName())) {
            // 同步更新其他关联表中的数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());

            // TODO 更新其他关联
        }
    }


}